package com.bj58.data.emailreports.sends;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.bj58.data.emailreports.model.DataSourceCheck;
import com.bj58.data.emailreports.model.EmailReportUser;
import com.bj58.data.emailreports.service.EmailReportUserService;
import com.bj58.data.emailreports.utils.DateUtil;
import com.bj58.data.emailreports.utils.MailUtil;
import com.bj58.data.emailreports.utils.SubmitSms;
import com.bj58.groupbuy.service.locator.ServiceLocator;

public class BaseSend {
	private EmailReportUserService emailReportUserService = ServiceLocator.get(EmailReportUserService.class);
	/**根据email_report_user中的sendtype获取发送用户对象
	 * @param sendType
	 * @return
	 * @throws Exception
	 */
	public List<EmailReportUser> getUsersBySendType(int sendType)throws Exception{
		return emailReportUserService.getListByCondition("sendtype="+sendType);//发送用户
	}
	
	
	/**邮件报告发送
	 */
	public void ReportSend(List<EmailReportUser> users,String title,String content,ArrayList<String>  attachedFileList){
		try {
			for(EmailReportUser user : users){
				String address =user.getEmail();
				int result = MailUtil.javaMailSend(title, content, address,attachedFileList);
				if(result == 1){
					System.out.println("Send email success! the E-mail is "+address);
				}else{
					title = " 【发送异常!】 "+title;
					MailUtil.javaMailSend(title, "Send email falied! the E-mail is "+address,"huangliang@58.com;zhaoxiang@58.com;guoxue@58.com",null);
					
					Map<String,String> telMap=new HashMap<String,String>();
			    	telMap.put("13522482231","email report error:"+title);
			    	telMap.put("13811583416","email report error:"+title);
			    	telMap.put("15726690526","email report error:"+title);
			    	SubmitSms.submitMsg(telMap);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			title = " 【发送异常!】 "+title;
			MailUtil.javaMailSend(title, "email report error ", "huangliang@58.com;zhaoxiang@58.com;guoxue@58.com",null);
			
			Map<String,String> telMap=new HashMap<String,String>();
	    	telMap.put("13522482231","email report error:"+title);
	    	telMap.put("13811583416","email report error:"+title);
	    	telMap.put("15726690526","email report error:"+title);
	    	SubmitSms.submitMsg(telMap);
	    	
		}
	}
	
	public void errorWarn(String content){
    	//邮件报警
    	MailUtil.javaMailSend("email report error", content, "huangliang@58.com;zhaoxiang@58.com;guoxue@58.com",null);
    	Map<String,String> telMap=new HashMap<String,String>();
    	telMap.put("13522482231","email report error:"+content);
    	telMap.put("13811583416","email report error:"+content);
    	telMap.put("15726690526","email report error:"+content);
    	SubmitSms.submitMsg(telMap);
	}
	
	
	
	/**数据源校验   
	 * @return
	 * @throws Exception
	 */
	public boolean checkDataSource(DataSourceCheck checkVo){
		try {
			
			TreeMap<String,Integer> rsMap = new TreeMap<String, Integer>();
			
			if(checkVo.getTableName().contains("*")){//按月OR年切分的表
				String tableName = checkVo.getTableName();
				String patter = tableName.substring(tableName.indexOf("*") + 1 ,tableName.lastIndexOf("*"));
				
				List<String>  subList = DateUtil.subDateByPatter(checkVo.getCheckStartDate(), checkVo.getCheckEndDate(), patter);
				for (String sub : subList) {
					tableName = checkVo.getTableName();
					tableName = tableName.substring(0,tableName.indexOf("*") + 1 ) + sub;
					checkVo.setTableName(tableName);
					rsMap.putAll(emailReportUserService.checkDataSource(checkVo));
				}
			}else{//单表
				rsMap.putAll(emailReportUserService.checkDataSource(checkVo));
			}
			
			
			//结果校验
			long dateCount = DateUtil.getDistDates(DateUtil.strToDate(checkVo.getCheckStartDate(), "yyyy-MM-dd"), DateUtil.strToDate(checkVo.getCheckEndDate(), "yyyy-MM-dd")) + 1;
			if(dateCount != rsMap.size()){ //日期数条数是否一致
				return false;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
		
	}
}
